package cn.doourbest.learnspring.classloader;

import java.io.*;
import java.nio.channels.Channel;
import java.nio.channels.spi.SelectorProvider;

/**
 * 为JavaClass劫持java.lang.System提供支持
 * 除了out和err外， 其余的都直接转发给System处理
 * *
 *
 * @author zzm
 */
public class HackSystem {
    public final static InputStream in = System.in;
    private static ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    public final static PrintStream out = new PrintStream(buffer);
    public final static PrintStream err = out;

    public static String getBufferString() {
        return buffer.toString();
    }

    public static void clearBuffer() {
        buffer.reset();
    }

    public static void setSecurityManager(final SecurityManager s) {
        System.setSecurityManager(s);
    }

    public static SecurityManager getSecurityManager() {
        return System.getSecurityManager();
    }

    public static long currentTimeMillis() {
        return System.currentTimeMillis();
    }

    public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length) {
        System.arraycopy(src, srcPos, dest, destPos, length);
    }

    public static int identityHashCode(Object x) {
        return System.identityHashCode(x);
    }

    public static void setIn(InputStream var0) {
        checkIO();
        setIn0(var0);
    }

    public static void setOut(PrintStream var0) {
        checkIO();
        setOut0(var0);
    }

    public static void setErr(PrintStream var0) {
        checkIO();
        setErr0(var0);
    }


    public static Channel inheritedChannel() throws IOException {
        return SelectorProvider.provider().inheritedChannel();
    }

    private static void checkIO() {
        SecurityManager var0 = getSecurityManager();
        if (var0 != null) {
            var0.checkPermission(new RuntimePermission("setIO"));
        }

    }

    private static native void setIn0(InputStream var0);

    private static native void setOut0(PrintStream var0);

    private static native void setErr0(PrintStream var0);



    private static void checkKey(String var0) {
        if (var0 == null) {
            throw new NullPointerException("key can't be null");
        } else if (var0.equals("")) {
            throw new IllegalArgumentException("key can't be empty");
        }
    }



    public static void exit(int var0) {
        Runtime.getRuntime().exit(var0);
    }

    public static void gc() {
        Runtime.getRuntime().gc();
    }

    public static void runFinalization() {
        Runtime.getRuntime().runFinalization();
    }


    public static native String mapLibraryName(String var0);

    private static PrintStream newPrintStream(FileOutputStream var0, String var1) {
        if (var1 != null) {
            try {
                return new PrintStream(new BufferedOutputStream(var0, 128), true, var1);
            } catch (UnsupportedEncodingException var3) {
            }
        }

        return new PrintStream(new BufferedOutputStream(var0, 128), true);
    }


//下面所有的方法都与java.lang.System的名称一样
//实现都是字节转调System的对应方法
//因版面原因， 省略了其他方法
}